--
-- Author: yawei.zhu
-- Date: 2018-12-17 14:54:44
--

local UserBaseInfo = require("app.public.dao.UserBaseInfo")

local PvpChallengeSeasonInfo = class("PvpChallengeSeasonInfo")

function PvpChallengeSeasonInfo:ctor(info)
	if info then
		self:read(info)
	end
end

function PvpChallengeSeasonInfo:read(info)
	self.id = info.id
	self.startTime = info.startTime
	self.endTime = info.endTime
	self.lv = info.lv
	self.score = info.score
	self.playId = info.playId
	self.myRank = info.myRank
	self.starCount = info.starCount
	self.winTimes = info.winTimes
	self.LastLevel = info.LastLevel
	self.LastParticipated = info.LastParticipated
	self.LastRewarded = info.LastRewarded -- 为true表示已领取
	self.dailyScore = info.dailyScore
	self.championBaseInfo = nil
	if info.championInfo then
		self.championBaseInfo = UserBaseInfo.new(info.championInfo)
	end

	self:readMatchInfo(info.matchInfo)
	
	local teamTarget = self:getTeamTarget()
	teamTarget:setChallengeInfo(self)
end

function PvpChallengeSeasonInfo:readMatchInfo(info)
	local matchInfo = {}
	matchInfo.state = info.state
	matchInfo.startStamp = info.startStamp
	local teammates = {}
	for _, data in ipairs(info.teammates) do
		local baseInfo = UserBaseInfo.new(data)
		table.insert(teammates, baseInfo)
	end
	matchInfo.teammates = teammates
	self.matchInfo = matchInfo

end

function PvpChallengeSeasonInfo:setState(state)
	self.matchInfo.state = state
end

function PvpChallengeSeasonInfo:test()
	local info = {}
	info.id = 1
	info.startTime = 1545113866
	info.endTime = 1545128266
	info.lv = 3
	info.score = 30
	info.playId = 904
	info.myRank = 5
	info.starCount = 3
	info.winTimes = 3
	info.LastLevel = 9
	info.LastParticipated = true
	info.LastRewarded = false
	info.dailyScore = 100

	local matchInfo = {}
	matchInfo.state = "idle"
	matchInfo.startStamp = "1547722767"
	matchInfo.teammates = {UD:getAvatarParams(), UD:getAvatarParams()}
	info.matchInfo = matchInfo

	self:read(info)
end

-- 挑战赛总玩法目标
-- return TeamTargetVo
function PvpChallengeSeasonInfo:getChallengeTarget()
	return app:sendMsg("TeamModel", "getTeamTargetById", Enums.TeamTarget.challenge)
end

-- 当前玩法目标
-- return TeamTargetVo
function PvpChallengeSeasonInfo:getTeamTarget()
	return app:sendMsg("TeamModel", "getTeamTargetById", self.playId)
end

function PvpChallengeSeasonInfo:getTitle()
	return L("targetname23")
end
function PvpChallengeSeasonInfo:getSeasonStr()
	return string.format(L("SuperRankSeasonStr"), self.id)
end

function PvpChallengeSeasonInfo:getFullTitle()
	return self:getTitle() .. " " .. self:getSeasonStr()
end

function PvpChallengeSeasonInfo:getDateStr()
	-- 转化为服务器的时间戳
	local beginDate = os.date("*t", self.startTime)
	local endDate = os.date("*t", self.endTime - 24 * 3600) -- 赛季显示7天，服务器发来的是8天的，所以显示时由客户端强制减1天！！！
	return string.format("%d.%d-%d.%d", beginDate.month, beginDate.day, endDate.month, endDate.day)
end

function PvpChallengeSeasonInfo:getBanner(isOpen)
	local target = self:getTeamTarget()
	return target:getBanner(isOpen)
end

function PvpChallengeSeasonInfo:getTargetName()
	local target = self:getTeamTarget()
	return string.format(L("challengeTips8"), target:getName())
end

function PvpChallengeSeasonInfo:getLvIcon()
	return string.format("%s%s.png", Res.eliteMatchPath, "war_elite_icon_" .. self.lv)
end

function PvpChallengeSeasonInfo:getScore()
	local levelInfo = GD:getChallengeLevelInfo(self.lv)
	local needScore = tonumber(levelInfo.levelupExp)
	local curScore = self.score - tonumber(levelInfo.totalExp)
	return curScore, needScore
end


function PvpChallengeSeasonInfo:getState()
	return self.matchInfo.state
end

function PvpChallengeSeasonInfo:getStartStamp()
	return self.matchInfo.startStamp
end

function PvpChallengeSeasonInfo:getTeammates()
	return self.matchInfo.teammates
end

function PvpChallengeSeasonInfo:clearLastSeasonReward()
	self.LastRewarded = true
end

function PvpChallengeSeasonInfo:getDailyScore()
	return self.dailyScore
end

function PvpChallengeSeasonInfo:getWinTimes()
	return self.winTimes
end

return PvpChallengeSeasonInfo

